package org.credo.net.study;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class GetPostTest {

	public static void main(String[] args) {
		String s=GetPostTest.sendGet("http://www.baidu.com/", null);
		System.out.println("s:"+s);
		
		String s2=GetPostTest.sendPost("http://www.baidu.com/", null);
		System.out.println("s2"+s2);
	}
	
	/**
	 * 可发送.
	 * 向指定URL发送一个Get请求
	 * @param urlStr 发送请求的url
	 * @param urlParam 请求参数,满足 "name1=value1&&name2=value2"的格式
	 * @return URL代表的远程资源的响应
	 */
	public static String sendGet(String urlStr, String urlParam) {
		String result = "";
		String realUrlStr = urlStr + "?" + urlParam;
		try {
			URL url = new URL(realUrlStr);
			// 打开和URL之间的连接
			URLConnection conn = url.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("Content-Type", "text/html;charset=utf-8");
			//conn.setRequestProperty("Cache-Control", "no-cache");
			//conn.setRequestProperty("Pragma", "no-cache");
			//conn.setRequestProperty("Expires", "-1");
			// 建立实际的连接
			conn.connect();
			// 获取所有的响应头字段
			Map<String, List<String>> map = conn.getHeaderFields();
			// 遍历所有的响应头字段
			for (String key : map.keySet()) {
				System.out.println(key + "===>" + map.get(key));
			}
			//System.out.println("======LionCredo=====");
			// 定义BufferedReader输入流来读取URL的响应
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
			String line;
			while ((line = in.readLine()) != null) {
				result += "\n" + line;
			}
		} catch (Exception e) {
			System.out.println("发送get请求出现异常!");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 未知,不知道是否正确.
	 * @param urlStr
	 * @param urlParam
	 * @return
	 */
	public static String sendPost(String urlStr, String urlParam) {
		String result = "";
		String realUrlStr = urlStr;
		try {
			URL url = new URL(realUrlStr);
			URLConnection conn = url.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("Content-Type", "text/html;charset=utf-8");
			// conn.setRequestProperty("Cache-Control", "no-cache");
			// conn.setRequestProperty("Pragma", "no-cache");
			// conn.setRequestProperty("Expires", "-1");
			//发送POST请求必须设置如下两行
			conn.setDoInput(true);
			conn.setDoOutput(true);
			//获取URLConnection对象对应的输出流
			PrintWriter out=new PrintWriter(conn.getOutputStream());
			out.flush();
			out.close();
			
			BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream(),"gbk"));
			String line;
			while((line=in.readLine())!=null){
				result +="\n"+line;
			}
		} catch (Exception e) {
			System.out.println("发送POST请求异常!");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 成功发送POST.
	 */
	public static void sendPostToRest() {
		try {
			String xmlpath = "D:\\restxml\\stay.xml";// xml文件保存路径
			// 读取xml文件
			BufferedReader fin = new BufferedReader(new FileReader(xmlpath));
			StringBuffer xmlBuffer = new StringBuffer();
			int k = 0;
			while (k != -1) {
				k = fin.read();
				if (k != -1)
					xmlBuffer.append((char) k);
			}
			fin.close();
			System.out.println("XML内容为:"+xmlBuffer.toString());
			String xmlStr = xmlBuffer.toString();
			String url2 = "http://localhost:8080/kunlun-integration/rest/OXIInterface?propertyName=credoTest&messageType=stay&status=123";
			java.net.URL pageUrl = new java.net.URL(url2);
			HttpURLConnection httpurlconnection = (HttpURLConnection) pageUrl.openConnection();
			httpurlconnection.setRequestMethod("POST");
			httpurlconnection.setDoInput(true);
			httpurlconnection.setDoOutput(true);
			
			httpurlconnection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
			httpurlconnection.setRequestProperty("Cache-Control", "no-cache");
			httpurlconnection.setRequestProperty("Pragma", "no-cache");
			httpurlconnection.setRequestProperty("Expires", "-1");
			
			httpurlconnection.setRequestProperty("Content-Length", "" + xmlStr.getBytes().length);
			System.out.println("lengthNum:"+xmlStr.getBytes().length);
			
		    httpurlconnection.getOutputStream().write(xmlStr.getBytes());
		    
			httpurlconnection.getOutputStream().flush();
		    httpurlconnection.getOutputStream().close();
			BufferedInputStream bis = new BufferedInputStream(httpurlconnection.getInputStream());
			bis.close();
			xmlBuffer = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
